(function (Cofe, Spine, $) {
    var Models = Cofe.Models;
    var File = Models.File;
    var Folder = Models.Folder;

    var EntryList = Spine.Model.setup("EntryList", ["status", "entries", "path"]);

    EntryList.extend(Spine.Model.Local);

    EntryList.include({
        loadProps: function (entryData) {
            var folderCount = 0;
            var fileCount = 0;
            var totalSize = 0;

            for (var i = 0, il = entryData.length; i < il; i++) {
                var currentEntry = entryData[i];
                var newEntry;
                if (currentEntry.basetype == "Folder") {
                    folderCount++;
                }
                else {
                    fileCount++;
                    if (currentEntry.length != undefined)
                        totalSize += currentEntry.length;
                }
            }

            this.totalCount = folderCount + fileCount;
            this.folderCount = folderCount;
            this.fileCount = fileCount;
        },

        load: function (data) {
            this.status = data.status;
            this.entries = data.entries;
            this.loadProps(data.entries);
        },
        init: function (data) {
            this.path = this.id = data.path;
            this.status = data.status;
            this.entries = data.entries;
            if (data.entries)
                this.loadProps(data.entries);
        }

    });

    EntryList.include({
        url: "",
        fetch: function () {
            var url = "/lookup/" + this.path + "?rel=list&json=true"
            var jqxhr = $.get(url,
                this.proxy(function (data) {
                    this.load(data); //load data.
                    this.save(); //save current entrylist to local storage.
                    EntryList.trigger("refresh", this);
                }))
                .error(function (xhr, s, e) { EntryList.trigger(xhr, s, e); })
        }
    });

    EntryList.extend({
        lookupOrCreate: function (path, refresh, ajaxErr) {
            var entryList;
            if (Cofe.Models.EntryList.exists(path)) {
                entryList = Cofe.Models.EntryList.select(function (el) { return el.id == path; })[0];
            } else {
                entryList = Cofe.Models.EntryList.init({ path: path });
                entryList.save(); //save it first.
            }
            EntryList.attachEvents(entryList, refresh, ajaxErr);

            if (entryList.status == undefined && refresh != undefined && ajaxErr != undefined) {
                entryList.fetch();
            }

            return entryList;
        },

        unattachEvents: function (prevEntryList, refresh, ajaxErr) {
            if (prevEntryList != undefined && prevEntryList.unbind != undefined) {
                if (refresh != undefined)
                    prevEntryList.unbind("refresh", refresh);
                if (ajaxError != undefined)
                    prevEntryList.unbind("ajaxError", ajaxErr);
            }
        },

        attachEvents: function (entryList, refresh, ajaxErr) {
            if (refresh != undefined)
                entryList.bind("refresh", refresh);
            if (ajaxErr != undefined)
                entryList.bind("ajaxError", ajaxErr);
        }


    });


    if (typeof exports !== "undefined") {
        Models.EntryList = exports;
    } else {
        Models.EntryList = EntryList;
    }

})(Cofe, Spine, Spine.$);